#Compile SP1CUDA for Linux
#using gcc and nvcc

ARCH=$(shell uname -m)
ifeq ($(ARCH),i686)
        ARCHPREFIX=
        MACHINE=-m32
else
        ARCHPREFIX=64
        MACHINE=-m64
endif

CC = gcc
CU = /usr/local/cuda/bin/nvcc
HOMEDIR = $(CURDIR)
OUTDIR = $(HOMEDIR)/out
TARGET = $(OUTDIR)/sp1cuda

INCLUDE = -I$(HOMEDIR) \
		  -I$(HOMEDIR)/inc/cudaqsort \
	      -I/usr/include \
	      -I/usr/local/cuda/include \
	      -I/home/$(USER)/NVIDIA_GPU_Computing_SDK/C/common/inc

SRCDIR=$(HOMEDIR)/src/core
QSSRC=$(HOMEDIR)/src/cudaqsort
LIBS =  -lcudart -lcurand -lgsl -lgslcblas -lm 
STATIC =
OPTIMIZATIONS = -O1
OMP=-fopenmp
#GCCDEBUG = -g
XCOMPILER = -Xcompiler $(OMP) 
CFLAGS = $(OPTIMIZATIONS) $(MACHINE) -DDEBUG -DVERBOSE -D__LINUX__ $(INCLUDE) 

COBJS =  $(OUTDIR)/main.o \
        $(OUTDIR)/ucgrid1d.o \
        $(OUTDIR)/mover1d.o \
        $(OUTDIR)/ebsources.o \
        $(OUTDIR)/lib.o \
        $(OUTDIR)/particles.o


LIBDIR = -L/usr/lib$(ARCHPREFIX) \
         -L/usr/local/cuda/lib$(ARCHPREFIX) \
		 -L$(OUTDIR)

LFLAGS = $(LIBDIR) \
		 $(LIBS)

EMUCUDA=
CUDAFASTMATH=-use_fast_math
	
all : sp1cuda

$(OUTDIR)/main.o :
	$(CU) $(CFLAGS) $(CUDAFASTMATH) $(EMUCUDA) -c -o $(OUTDIR)/main.o $(HOMEDIR)/main.c $(XCOMPILER)

$(OUTDIR)/ucgrid1d.o :
	$(CU) $(CFLAGS) $(CUDAFASTMATH) $(EMUCUDA) -c -o $(OUTDIR)/ucgrid1d.o $(SRCDIR)/ucgrid1d.cu $(XCOMPILER)

$(OUTDIR)/mover1d.o :
	$(CU) $(CFLAGS) $(CUDAFASTMATH) $(EMUCUDA)  -c -o $(OUTDIR)/mover1d.o $(SRCDIR)/mover1d.cu $(XCOMPILER)

$(OUTDIR)/ebsources.o :
	$(CU) $(CFLAGS) $(CUDAFASTMATH) $(EMUCUDA)  -c -o $(OUTDIR)/ebsources.o $(SRCDIR)/ebsources.c $(XCOMPILER)

$(OUTDIR)/lib.o :
	$(CU) $(CFLAGS) $(CUDAFASTMATH) $(EMUCUDA)  -c -o $(OUTDIR)/lib.o $(SRCDIR)/lib.c $(XCOMPILER)

$(OUTDIR)/particles.o :
	$(CU) $(CFLAGS) $(CUDAFASTMATH) $(EMUCUDA)  -c -o $(OUTDIR)/particles.o $(SRCDIR)/particles.cu  $(XCOMPILER)
	

sp1cuda : $(COBJS)
	$(CU) $(CFLAGS)  $(CUDAFASTMATH) $(EMUCUDA) $(COBJS) -o $(TARGET) $(LFLAGS) $(STATIC) $(XCOMPILER)
clean :
	rm -rf $(COBJS)
	rm -rf $(TARGET)





